/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.managementelements;

import java.util.ArrayList;

import org.openaecfm.model.cost.CostValue;
import org.openaecfm.model.kernel.Control;
import org.openaecfm.model.managementelements.CostItem;
import org.openaecfm.model.managementelements.CostItemTypeEnum;
import org.openaecfm.model.quantity.PhysicalQuantity;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class CostItemIO extends AbstractEntityInstanceInputImpl<CostItem>
		implements IEntityInstanceOutput<CostItem> {
	private IEntityInstanceUtilInput<Control> utilInput;
	private IEntityInstanceUtilOutput<Control> utilOutput;

	public void setUtilOutput(IEntityInstanceUtilOutput<Control> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(CostItem pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCCOSTITEM",
				appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set PredefinedType
		if (pojo.getPredefinedType() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EnumerationParameter(pojo.getPredefinedType()));
		}

		// Set CostValues
		if (pojo.getCostValues() == null || pojo.getCostValues().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (CostValue c : pojo.getCostValues()) {
				l.add(new EntityInstanceNameParameter(appDataManager
						.getName(c)));
			}
			ei.addParameter(l);
		}

		// Set CostQuantities
		if (pojo.getCostQuantities() == null
				|| pojo.getCostQuantities().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (PhysicalQuantity p : pojo.getCostQuantities()) {
				l.add(new EntityInstanceNameParameter(appDataManager
						.getName(p)));
			}
			ei.addParameter(l);
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected CostItem doCreateObject() {
		return new CostItem();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, CostItem pojo,
			IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set CostValues
		Parameter costValues = sei.getParameter(7);
		if (costValues.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) costValues;
			if (pojo.getCostValues() == null) {
				pojo.setCostValues(new ArrayList<CostValue>());
			}
			for (Parameter p : l) {
				EntityInstanceNameParameter e = (EntityInstanceNameParameter) p;
				pojo.getCostValues()
						.add((CostValue) appDataManager.getObject(e
								.toInstanceName()));
			}
		}

		// Set CostQuantities
		Parameter costQuantities = sei.getParameter(8);
		if (costQuantities.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) costQuantities;
			if (pojo.getCostQuantities() == null) {
				pojo.setCostQuantities(new ArrayList<PhysicalQuantity>());
			}
			for (Parameter p : l) {
				EntityInstanceNameParameter e = (EntityInstanceNameParameter) p;
				pojo.getCostQuantities().add(
						(PhysicalQuantity) appDataManager.getObject(e
								.toInstanceName()));
			}
		}
	}

	public void setUtilInput(IEntityInstanceUtilInput<Control> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei, CostItem pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set PredefinedType
		Parameter predefinedType = sei.getParameter(6);
		if (predefinedType.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) predefinedType;
			pojo.setPredefinedType(CostItemTypeEnum.valueOf(e.toEnumString()));
		}
	}
}
